/**
 * Created by 13296 on 2018/7/7.
 */
/*更具视频类型显示视频*/
/**
 * Created by 13296 on 2018/7/4.
 */
/**********************主函数***********************/
//视频节点加载函数
let initVideo=function (video,node) {
    let s = '<div class="col-md-3 resent-grid recommended-grid">'+
        '   <div class="resent-grid-img recommended-grid-img">'+
        '       <a href="'+getRootPath_web()+'/video/single?video_id='+video.video_id+'"><img src="'+video.image_url+'" alt="" /></a>'+
        '       <div class="time small-time">'+
        '           <p>'+video.total_time+'</p>'+
        '       </div>'+
        '       <div class="clck small-clck">'+
        '           <span class="glyphicon glyphicon-time" aria-hidden="true"></span>'+
        '       </div>'+
        '   </div>'+
        '   <div class="resent-grid-info recommended-grid-info video-info-grid">'+
        '       <h5><a href="'+getRootPath_web()+'/video/single?video_id='+video.video_id+'" class="title">'+video.title+'</a></h5>'+
        '       <ul>'+
        '           <li><p class="author author-info"><a href="#" class="author">'+video.account+'</a></p></li>'+
        '           <li class="right-list"><p class="views views-info">'+video.views+' views</p></li>'+
        '       </ul>'+
        '   </div>'+
        '</div>';
    if(node != "#"){
        $(node).append(s);
    }
    return s;
};


//查看历史记录视频
let history_videos=function (user_id){
    let url ="/history/history_video";
    let method = "GET";
    let data = {
        user_id:user_id,
    };
    ajax_base(data, url, method, "Json").then(function (data) {
            if (data.success) {
                let videos = data.obj;
                console.log(videos);
                let s = '';
                let node = '#';
                for(var i = 0; i < videos.length; i++) {
                    s += initVideo(videos[i], node);
                    if (i%4==3  || i == (videos.length - 1)) {
                        s += '<div class="clearfix"> </div>';
                        let insert_str = '<div class="recommended-grids">' + s + '</div>';
                        // console.log(insert_str);
                        $(".recommended").append(insert_str);
                        s = '';
                    }
                }

            }
        }
    );
};

//根据视频类型展示视频
let display_videos=function (video_type,pn){
    let url = "/video/videos";
    let method = "GET";
    let data = {
        video_type:video_type,
        pageNum:pn,
    };
    ajax_base(data, url, method, "Json").then(function (data) {
            if (data.success) {
                let videos = data.obj;
                console.log(videos);
                for(var i = 0; i < videos.length; i++) {
                    if(i<4){
                        let node = '#video_show1';
                        initVideo(videos[i],node);
                        if(i==3 || i==(videos.length-1)){
                            $(node).append('<div class="clearfix"> </div>');
                        }
                    }else{
                        let node = '#video_show2';
                        initVideo(videos[i],node);
                        if(i==7 || i==(videos.length-1)){
                            $(node).append('<div class="clearfix"> </div>');
                        }
                    }
                }

            }
        }
    );
};

let pn = 2;
//滚动条触底刷新
let scrollBottom =function(video_type){
    var minAwayBtm = 2;  // 距离页面底部的最小距离
    $(window).scroll(function() {
        if(scroll_flag ==0) {//分类滚轮事件有效
            var awayBtm = $(document).height() - $(window).scrollTop() - $(window).height();
            // console.log('当前距离页面底部：' + awayBtm + 'px');
            if (awayBtm <= minAwayBtm) {
                // console.log('触发了'+pn.toString());
                let url = "/video/videos";
                let method = "GET";
                let data = {
                    video_type: video_type,
                    pageNum: pn,
                };
                pn++;
                ajax_base(data, url, method, "Json").then(function (data) {
                        if (data.success) {
                            let videos = data.obj;
                            // console.log(videos);
                            let s = '';
                            let node = '#';
                            if (videos.length != 0) {
                                for (var i = 0; i < videos.length; i++) {
                                    if (i < 4) {
                                        s += initVideo(videos[i], node);
                                        if (i == 3 || i == (videos.length - 1)) {
                                            s += '<div class="clearfix"> </div>';
                                            let insert_str = '<div class="recommended-grids">' + s + '</div>';
                                            // console.log(insert_str);
                                            $(".recommended").append(insert_str);
                                            s = '';
                                        }
                                    } else {
                                        s += initVideo(videos[i], node);
                                        if (i == 7 || i == (videos.length - 1)) {
                                            s += '<div class="clearfix"> </div>';
                                            let insert_str = '<div class="recommended-grids">' + s + '</div>';
                                            // console.log(insert_str);
                                            $(".recommended").append(insert_str);
                                            s = '';
                                        }
                                    }
                                }
                            } else {
                                my_alert(200, "已经加载全部！");
                            }
                        }
                    }
                );
            }
        }
    });
};


//加载主页面
let initRun=function () {
    scroll_flag = 0;//分类滚轮有效
    let video_type = getUrlParam("video_type");
    let pageNum = 1;
    console.log(video_type);
    console.log(pageNum);
    if(video_type =="history"){
        let user_id = get_current_user().user_id;
        if(user_id!=0){
            $("#video_type").text("历史记录");
            history_videos(user_id);
        }else{
            my_alert(200,"登录后才可以查看！");
        }
    }else{
        $("#video_type").text(video_type);
        display_videos(video_type,pageNum);
        //滚动条事件
        scrollBottom(video_type);
    }
};initRun();